#include "thread_safe_queue.h"
#include "client_manage.h"
#include <thread>
#include <atomic>

static std::vector<std::thread> thread_pool;

ThreadSafeQueue<Task> task_queue;

extern void send_data(int fd, const std::vector<char> &data);
extern ServerState g_state;
// 工作线程函数
void worker_thread() {
    while (g_state.running) {
        Task task;
        if (task_queue.pop(task)) {
            send_data(task.fd, task.data);
        }
    }
    std::cout << "worker thread exit" << std::endl;
}

void init_thread_pool(int thread_pool_size) {
    // 创建线程池

    for (int i = 0; i < thread_pool_size; ++i) {
        thread_pool.emplace_back(worker_thread);
    }
}

void destroy_thread_pool() {
     for (auto &thread : thread_pool) {
        if (thread.joinable()) {
            thread.join();
        }
    }
    thread_pool.clear();
    task_queue.stop();
}

void add_task(const Task &task) {
    task_queue.push(task);
};